const { ccclass, property } = cc._decorator;
import MyEnums from '../MyEnums';

@ccclass
export default class RefreshNodeControl extends cc.Component {
    @property()
    reinforce: number = 0.2;//强化系数
    @property()
    impair: number = -0.1;//虚弱系数
    @property([cc.NodePool])
    nodePools: cc.NodePool[] = [];
    @property([cc.NodePool])
    nodePools_: cc.NodePool[] = [];

    @property([cc.Prefab])
    prefabNodes: cc.Prefab[] = [];
    @property([cc.Prefab])
    prefabNodes_: cc.Prefab[] = [];

    @property(cc.NodePool)
    skillValuePools: cc.NodePool = null;
    @property(cc.Prefab)
    skillValuePrefab: cc.Prefab = null;

    @property
    RefreshTypeCount: number = 13;

    getCoffe(attribute: number, AttributeList: boolean[]): number {
        let coefficient = 1;
        switch (attribute) {
            case MyEnums.SkillAttribute.Fire://克水，生风
                {
                    if (AttributeList[MyEnums.SkillAttribute.Water]) {
                        return 0;
                    }
                    if (AttributeList[MyEnums.SkillAttribute.Wind]) {
                        coefficient += this.reinforce;
                    }
                    break;
                }
            case MyEnums.SkillAttribute.Water://克火，弱毒
                {
                    if (AttributeList[MyEnums.SkillAttribute.Fire]) {
                        return 0;
                    }
                    if (AttributeList[MyEnums.SkillAttribute.Poison]) {
                        coefficient += this.impair;
                    }
                    break;
                }
            case MyEnums.SkillAttribute.Wind://风助火
                {
                    if (AttributeList[MyEnums.SkillAttribute.Fire]) {
                        coefficient += this.reinforce;
                    }
                    break;
                }
        }
        return coefficient;
    }
    initNodes(RefreshPrefabs: Map<string, cc.Prefab>) {
        for (let i = 0; i < this.RefreshTypeCount; i++) {
            let key = MyEnums.SkillRefreshType[i];
            let pfb = RefreshPrefabs.get(key);
            let pfb_ = RefreshPrefabs.get(key + "_");
            if (pfb) {
                this.prefabNodes[i] = pfb;
                this.prefabNodes_[i] = pfb_;
            }
            else {
                this.prefabNodes[i] = null;
                this.prefabNodes_[i] = null;
            }
        }
        this.skillValuePrefab = RefreshPrefabs.get("skillValue");
    }
    getNodePool(RefreshType: number): cc.Node {
        let node = this.nodePools[RefreshType];
        if (node?.size() > 0) {
            return node.get();
        }
        else {
            if (this.prefabNodes[RefreshType])
                return cc.instantiate(this.prefabNodes[RefreshType]);
            return null;
        }
    }
    setNodePool(RefreshType: number, node: cc.Node) {
        let nodepool = this.nodePools[RefreshType];
        if (nodepool) {
            nodepool.put(node);
        }
        else {
            let newpool: cc.NodePool = new cc.NodePool();
            newpool.put(node);
            this.nodePools[RefreshType] = newpool;
        }
    }
    getNodePool_(RefreshType: number): cc.Node {
        let node = this.nodePools_[RefreshType];
        if (node?.size() > 0) {
            return node.get();
        }
        else {
            if (this.prefabNodes_[RefreshType])
                return cc.instantiate(this.prefabNodes_[RefreshType]);
            return null;
        }
    }
    setNodePool_(RefreshType: number, node: cc.Node) {
        let nodepool = this.nodePools_[RefreshType];
        if (nodepool) {
            nodepool.put(node);
        }
        else {
            let newpool: cc.NodePool = new cc.NodePool();
            newpool.put(node);
            this.nodePools_[RefreshType] = newpool;
        }
    }
    getSkillValuePool(): cc.Node {
        if (this.skillValuePools?.size() > 0) {
            return this.skillValuePools.get();
        }
        else {
            if (this.skillValuePrefab)
                return cc.instantiate(this.skillValuePrefab);
            return null;
        }
    }
    setSkillValuePool(node: cc.Node) {
        if (!this.skillValuePools) {
            this.skillValuePools = new cc.NodePool();
        }
        this.skillValuePools.put(node);
    }
}
